home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / src / geom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  3.5 KB  |  179 lines

  1. #include "HEADERS.h"
  2.  
  3. #include "srgp.h"
  4. #include "geom.h"
  5.  
  6.  
  7. /*!*/
  8. srgp__point GEOM_sumOfPoints (srgp__point pt1, srgp__point pt2)
  9. {
  10.    point psum;
  11.  
  12.    psum.x = pt1.x + pt2.x;
  13.    psum.y = pt1.y + pt2.y;
  14.    return psum;
  15. }
  16.  
  17.  
  18.  
  19. /*!*/
  20. int GEOM_widthOfRect (srgp__rectangle r)
  21. {
  22.    return r.top_right.x - r.bottom_left.x + 1;
  23. }
  24.  
  25. int GEOM_heightOfRect (srgp__rectangle r)
  26. {
  27.    return r.top_right.y - r.bottom_left.y + 1;
  28. }
  29.  
  30.  
  31.  
  32.  
  33. /*!*/
  34. static void
  35. GEOM_calc_intersection_on_1_axis
  36.    (int min_a, int max_a, int min_b, int max_b, 
  37.     int *does_intersect, int *min_c, int *max_c)
  38. {
  39.    int min_t, max_t;
  40.  
  41.    /* FIRST, REARRANGE a && b SO THAT mina <== minb */
  42.    if (! (min_a <= min_b)) {
  43.       min_t = min_a;  max_t = max_a;
  44.       min_a = min_b;  max_a = max_b;
  45.       min_b = min_t;  max_b = max_t;
  46.    }
  47.  
  48.    /* THEN, PERFORM INTERSECTION */
  49.    if (min_b > max_a) 
  50.       *does_intersect = FALSE;
  51.    else {
  52.       *does_intersect = TRUE;
  53.       *min_c = min_b;
  54.       if (max_a < max_b) 
  55.      *max_c = max_a;
  56.       else
  57.      *max_c = max_b;
  58.    }
  59. }
  60.       
  61.  
  62.  
  63. /*!*/
  64. int GEOM_computeRectIntersection 
  65.    (srgp__rectangle r1, srgp__rectangle r2, 
  66.     srgp__rectangle *result)
  67. {
  68.    int does_intersect;
  69.  
  70.    /* first: X axis */
  71.    GEOM_calc_intersection_on_1_axis
  72.       (r1.bottom_left.x, r1.top_right.x, r2.bottom_left.x, r2.top_right.x,
  73.        &does_intersect, 
  74.        &(result->bottom_left.x), &(result->top_right.x));
  75.  
  76.    if (does_intersect)
  77.       /* then: Y axis */
  78.       GEOM_calc_intersection_on_1_axis
  79.      (r1.bottom_left.y, r1.top_right.y, r2.bottom_left.y, r2.top_right.y,
  80.       &does_intersect, 
  81.       &(result->bottom_left.y), &(result->top_right.y));
  82.  
  83.    return does_intersect;
  84. }
  85.  
  86.  
  87.  
  88.  
  89. /*!*/
  90. void
  91. GEOM_computeRectUnion 
  92.    (srgp__rectangle r1, srgp__rectangle r2, 
  93.     srgp__rectangle *result)
  94. {
  95.    *result = r2;
  96.    if (r1.bottom_left.x < result->bottom_left.x) 
  97.       result->bottom_left.x = r1.bottom_left.x;
  98.    if (r1.top_right.x > result->top_right.x) 
  99.       result->top_right.x =  r1.top_right.x;
  100.    if (r1.bottom_left.y < result->bottom_left.y) 
  101.       result->bottom_left.y = r1.bottom_left.y;
  102.    if (r1.top_right.y > result->top_right.y) 
  103.       result->top_right.y =  r1.top_right.y;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. /*!*/
  110. srgp__rectangle GEOM_rectWithCommonCenter 
  111.    (srgp__rectangle r, int rect_width, int rect_height)
  112. {
  113.    rectangle rect_with_common_center;
  114.  
  115.    rect_with_common_center.bottom_left = 
  116.       GEOM_sumOfPoints
  117.      (r.bottom_left,
  118.       SRGP_defPoint
  119.          ((GEOM_widthOfRect(r) - rect_width) >> 1,
  120.           (GEOM_heightOfRect(r) - rect_height) >> 1));
  121.  
  122.    rect_with_common_center.top_right =
  123.       GEOM_sumOfPoints
  124.      (rect_with_common_center.bottom_left,
  125.       SRGP_defPoint (rect_width-1, rect_height-1));
  126.  
  127.    return rect_with_common_center;
  128. }
  129.  
  130.  
  131.  
  132.  
  133. /*!*/
  134. srgp__rectangle GEOM_rectFromDiagPoints (srgp__point pt1, srgp__point pt2)
  135. {
  136.    rectangle result;
  137.  
  138.    if (pt1.x < pt2.x) 
  139.       {
  140.      result.bottom_left.x = pt1.x;
  141.      result.top_right.x = pt2.x;
  142.       }
  143.    else
  144.       {
  145.      result.top_right.x = pt1.x;
  146.      result.bottom_left.x = pt2.x;
  147.       }
  148.  
  149.    if (pt1.y < pt2.y) 
  150.       {
  151.      result.bottom_left.y = pt1.y;
  152.      result.top_right.y = pt2.y;
  153.       }
  154.    else
  155.       {
  156.      result.top_right.y = pt1.y;
  157.      result.bottom_left.y = pt2.y;
  158.       }
  159.    
  160.    return result;
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /*!*/
  167. /** GEOM PT IN RECT
  168. This function strains for efficiency by exiting as soon as a
  169. reject can be made.  The "nicer" Pascal version (using the &&
  170. conjunction and only one if statement) would not be efficient.
  171. **/
  172. int GEOM_ptInRect (srgp__point pt, srgp__rectangle r)
  173. {
  174.    return (pt.x >= r.bottom_left.x) &&
  175.           (pt.x <= r.top_right.x) &&
  176.       (pt.y >= r.bottom_left.y) &&
  177.       (pt.y <= r.top_right.y);
  178. }
  179.